import { VueComponent,Prop } from 'vue-typescript'
import * as VueRouter from 'vue-router'
import {dataService} from '../../service/dataService'
Vue.use(VueRouter);
var VuePagination = require('v-pagination');
Vue.use(VuePagination)
// declare $table.bootstrapTable('load', data);
interface JQuery {
    bootstrapTable(options?: any): JQuery;
    bootstrapTable(name:string,any):JQuery;
}
declare var bootstrapTable: JQueryStatic;
var router = new VueRouter();
var VueTables = require('vue-tables');
Vue.use(VueTables.client,{
    filterable:false,
    compileTemplates:true,
    highlightMatches:true
});
@VueComponent({
    template: require('./inquiryReleaseManage.html')
})
export class InquiryReleaseManageComponent extends Vue {
    el:'#people'
    components:({
        vclienttable:any,
    })
    @Prop
    Records=1
    PerPage=10
    showRecords=true;
    ready=function(){
        $('p').remove(".VuePagination__count");
        this.searchingStartTime='';
        this.searchingEndTime='';
        this.searchingStartAddress='';
        this.searchingEndAddress='';
        this.searchingInquiryId='';
        this.searchingInquiryChildId='';
        this.searchingState=''
        this.$on('vue-pagination::table', function(page) {
            this.table1Page = page;	//当前页数
            // console.info(page)
            this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,(page-1)*this.count,this.count);
        });
        $('#StartTime').datetimepicker({format:  'YYYY.MM.DD HH:mm'});
        $("#EndTime").datetimepicker({format:  'YYYY.MM.DD HH:mm'});
         var $table = $('#table').bootstrapTable({
                // url: "index.php",//数据源
                dataField: "rows",//服务端返回数据键值 就是说记录放的键值是rows，分页时使用总记录数的键值为total
                // height: tableHeight(),
                clickToSelect:true,
                singleSelect:true,
                pagination: true,//是否分页
                pageSize: 10,//单页记录数
                pageList: [10, 20, 30],//分页步进值
                sidePagination: "client",//服务端分页
                buttonsAlign: "left",//按钮对齐方式 子询价编号 发货地址 送货地址 货物名称 货物数量 所需车长 询价时间 紧急程度 询价状态
                columns: [
                    {field: "select",title: "全选",checkbox: true,width: 20,align: "center",valign: "middle"},
                    {field: "cspInquiryId",title: "总询价编号", sortable: true,order: "desc"},
                    {field: "cspInquiryChildId",title: "子询价编号",sortable: true,titleTooltip: "this is name"},
                    {field: "startAddress",title: "发货地址",sortable: true,},
                    {field: "endAddress",title: "送货地址",sortable: true,},
                    {field: "goodsName",title: "货物名称",sortable: true,},
                    {field: "goodsNum",title: "货物数量",sortable: true,},
                    {field: "carLength",title: "所需车长",sortable: true,},
                    {field: "cspInquiryTime",title: "询价时间",sortable: true,},
                    {field: "responseTime",title: "紧急程度",sortable: true,},
                    {field: "status",title: "询价状态",sortable: true,},
                    // {field: "info",title: "INFO[using-formatter]"},
                    {
                        field: 'template',
                        title: '操作',
                        formatter: function operateFormatter(value, row, index) {
                            // Chris=='待处理' Dan=="已处理"
                            var aa=`<a title='终结订单' class="endOrder"><i class='glyphicon glyphicon-minus-sign primary text-primary m-l-xs'></i></a>`;
                            var bb=`<a class="detailOrder" href='#!/inquiry/inquiryAdd/?id=${row.id}&name=detail' title='详情'><i class='glyphicon glyphicon-eye-open m-l-xs primary text-info'></i></a>`;
                            var cc=`<a href='#!/inquiry/inquiryAdd/?id=${row.id}&name=edit' title='编辑'><i class='glyphicon glyphicon-edit m-l-xs primary text-info'></i></a>`;
                            var dd=`<a class='remove' title='删除' href='javascript:void(0);'><i class='glyphicon glyphicon-trash  primary text-danger m-l-xs'></i></a>`;
                            switch(row.status){
                                case'未处理':return aa+``+bb+``+dd;
                                case'已处理':return cc;
                                default:return bb;
                            }
                        },
                        events: {
                            'click .endOrder':  (e, value, row, index)=> {
                                // alert('You click like action, row: ' + JSON.stringify(row));
                                bootbox.confirm("是否终结该订单？",(bootboxResult)=>{
                                    if(bootboxResult){
                                        dataService().CspInquiry.editCspInquiry(row.id).then((res)=>{
                                            // console.info('res:',res);
                                            // this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,this.skip,this.count);
                                            this.$refs.table.setPage(1);
                                        },(rej)=>{
                                            // console.info('rej:',rej);
                                        });
                                    }else{
                                        return;
                                    }
                                })
                            },
                            // 'click .detailOrder':function(e,value,row,index){
                            //      router.go('inquiryAdd/?id=${row.id}&name=detail');
                            // },
                            'click .remove':(e,value,row,index)=>{
                                // console.info('del');
                                bootbox.confirm("是否删除该订单？",(bootboxResult)=>{
                                    if(bootboxResult){
                                        dataService().CspInquiry.deleteCspInquiry(row.id).then((res)=>{
                                            // console.info('res:',res);
                                            // this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,this.skip,this.count);
                                            this.$refs.table.setPage(1);
                                        },(rej)=>{
                                            // console.info('rej:',rej);
                                        });
                                    }else{
                                        return;
                                    }
                                })
                            },
                        },
                    }
                ],
                data: [],
                onPageChange:(number, size)=>{
                    // 0,10,20,30
                    // console.info('number',(number-1)*size);
                    // console.info('size',size);
                    
                    this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,(number-1)*size,size);
                },
                onClickRow: (row, $element)=> {
                    //$element是当前tr的jquery对象
                    // $element.css("background-color", "green");
                    // console.info('!row["select"]',!row["select"]);
                    if(!row["select"]){
                        $('#btnLinToOrderAdd').removeAttr('disabled');
                    }else{
                        $('#btnLinToOrderAdd').attr('disabled', 'true');
                    }
                    // console.info(row);
                },//单击row事件
                actionFormatter:function(value, row, index){
                    // console.info('tyv')
                },
                locale: "zh-CN"//中文支持,
            });
        //表格 初始 加载数据 'MM/YYYY':
         this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,this.skip,this.count);
     }
    @Prop
    skip=0;
    count=10;
    router=new VueRouter();
    seeks = true;     
    //查询数据初始化
    searchingStartTime:string='';
    searchingEndTime:string='';
    searchingStartAddress:string='';
    searchingEndAddress:string='';
    @Prop
    searchingInquiryId:string='';
    searchingInquiryChildId:string='';
    searchingState=''
    StateDropDown=[{'text':'未处理','value':0},{'text':'已处理','value':1},{'text':'终结','value':2}]
   
    //请求数据
    load=function(searchingInquiryId,searchingInquiryChildId,searchingState,searchingStartTime,searchingEndTime,searchingStartAddress,searchingEndAddress,skip,count){
        dataService().CspInquiry.getCspInquiryList(searchingInquiryId,searchingInquiryChildId,searchingState,searchingStartTime,searchingEndTime,searchingStartAddress,searchingEndAddress,skip,count)
        .then((res)=>{
            // console.info('res:',res.data);                
            $('#table').bootstrapTable('load', res.data);
            // this.Records=res.total;total==0?0.5:total
            this.seeks=false;
            var totalItems=res.total;
            this.Records= totalItems==0?0.5:totalItems;
            this.showRecords=totalItems==0?false:true;
        },function(rej){
            // console.info('rej:',rej);
            this.seeks=false;
        });
    //   return this.Records=70;
    }
    //查询调用
    queryUsers=function(){
        this.seeks=true;
        // this.load(this.searchingInquiryId,this.searchingInquiryChildId,this.searchingState,this.searchingStartTime,this.searchingEndTime,this.searchingStartAddress,this.searchingEndAddress,this.skip,this.count);
        this.$refs.table.setPage(1);
    }
    
    deleteMe=function(id){
        //调后台接口(删除)
        // console.info('test');
    }
    
    //询价单新增
    LinckToInquiryAdd=function(){
        // console.info('rt:',$('#table').bootstrapTable('getSelections'));
        var rowSelected;
        rowSelected=$('#table').bootstrapTable('getSelections')[0];
        if($('#table').bootstrapTable('getSelections').length>0){
            router.go('inquiryAdd/?id='+rowSelected.id+'&name=copy');
        }else{
            router.go('inquiryAdd/?id='+'&name=add');
        }
        // console.info("this.seeks",this.seeks)
    }
    //订单新增按钮点击触发
    LinckToOrderAdd=function(){
        var rowSelectedToOrder;
        rowSelectedToOrder=$('#table').bootstrapTable('getSelections')[0];
        if($('#table').bootstrapTable('getSelections').length>0){
            router.go('../../order/orderReleaseAdd/?id='+rowSelectedToOrder.id+'&name=copyInquiryRelease');
        }
    }

}
